﻿# include "vd.h"
//#pragma code_seg()

NTSTATUS VD_ReadWrite (
    IN PDEVICE_OBJECT   DeviceObject,
    IN PIRP             Irp
    )
{
    
	PDEVICE_EXTENSION   device_extension;
    PIO_STACK_LOCATION  io_stack;
	PUCHAR              system_buffer;
    PUCHAR              buffer;
	NTSTATUS			status;
	
    device_extension = (PDEVICE_EXTENSION) DeviceObject->DeviceExtension;
	
	if (!device_extension->disk_mounted )
    {
		Irp->IoStatus.Status = STATUS_NO_MEDIA_IN_DEVICE;
        Irp->IoStatus.Information = 0;
		IoCompleteRequest(Irp, IO_NO_INCREMENT);
		return STATUS_NO_MEDIA_IN_DEVICE;
    }

    io_stack = IoGetCurrentIrpStackLocation(Irp);

    if (io_stack->Parameters.Read.Length == 0)
    {
        Irp->IoStatus.Status = STATUS_SUCCESS;
        Irp->IoStatus.Information = 0;

        IoCompleteRequest(Irp, IO_NO_INCREMENT);
	    return STATUS_SUCCESS;
    }
	
	IoMarkIrpPending(Irp);

    ExInterlockedInsertTailList(
							  &device_extension->list_head,
							  &Irp->Tail.Overlay.ListEntry,
							  &device_extension->list_lock
							   );
	
    KeSetEvent(
			 &device_extension->request_event,
			 (KPRIORITY) 0,
			 FALSE
			  );
	
	return STATUS_PENDING;
}
